﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Win32;

namespace gui {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {

        // Define RoutedCommands for shortcuts
        public static RoutedCommand ConnectCommand = new RoutedCommand();
        public static RoutedCommand SaveConsoleCommand = new RoutedCommand();
        public static RoutedCommand ExitCommand = new RoutedCommand();



        public MainWindow() {

            InitializeComponent();

            // Define shortcut keys
            CommandBinding connectBinding = new CommandBinding(ConnectCommand, Connect, CanExecuteCustomCommand);
            ConnectCommand.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control));
            CommandBinding saveConsoleBinding = new CommandBinding(SaveConsoleCommand, SaveConsole, CanExecuteCustomCommand);
            SaveConsoleCommand.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Control));
            CommandBinding exitBinding = new CommandBinding(ExitCommand, Exit, CanExecuteCustomCommand);
            ExitCommand.InputGestures.Add(new KeyGesture(Key.Q, ModifierKeys.Control));

        }

        public void Connect(object sender, ExecutedRoutedEventArgs e) {
            ConnectBox box = new ConnectBox();
            box.Show();
        }

        public void SaveConsole(object sender, ExecutedRoutedEventArgs e) {
            string filename = "";
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Select directory for algorithm output files";
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (.txt)|*.txt";
            if (dlg.ShowDialog() == true) {
                filename = dlg.FileName;
            }
            AlgorithmsTab.SaveConsoleOutput(filename);
        }

        public void Exit(object sender, ExecutedRoutedEventArgs e) {
            MessageBoxResult answer = MessageBox.Show("Are you sure you want to exit? Any unsaved results will be lost.", "Close NAV", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (answer == MessageBoxResult.Yes) Application.Current.Shutdown();
        }

        public void CanExecuteCustomCommand(object sender, CanExecuteRoutedEventArgs e) {
            Control target = e.Source as Control;
            if (target == null) {
                e.CanExecute = false;
                return;
            }
            e.CanExecute = true;
        }


    }
}